/**
 * Created by apple on 16/3/31.
 */
class Controller {
    constructor (systemService) {
        "ngInject"
        this.service = systemService;
        this.select  = {}
        this.data    = {}
        this.query   = false;
        this.region    = {
            commonRegionId: '',
            parRegionId   : 0,
            statusCd      : 'S0A'
        }
        this.regionMap = {
            1000: 'group',
            1100: 'province',
            1300: 'city',
            1400: 'area'
        }
        this.getRegion()
        console.log(this)
    }

    checkValid (field) {
        if ( field.$pristine ) {
            field.$pristine = false;
        }
    }

    getRegion (type = 1000, value = null) {
        if ( type ) {
            this.region.regionType = type
        }
        if ( value ) {
            this.ngModel = this.data.commonRegionId = value.id ? value.id : value;
            this.region.parRegionId  = value.id ? value.id : value;
        }

        if (this.query) {
            this.isEdit = false;
            this.region.qFlag = 'query';
            delete this.region.commonRegionId;
        }

        this.service.getRegion(this.region, this.isEdit).then((r) => {
            __DEBUG__ && console.log(r)
            if ( r.data ) {
                if ( !type ) {
                    this.select.group       = r.data;
                    this.region.parRegionId = r.data[ 0 ].id
                }
                if ( this.isEdit ) {
                    this.query = true;
                    this.select.group    = r.data[ '1000' ] ? r.data[ '1000' ].data : '';
                    this.select.province = r.data[ '1100' ] ? r.data[ '1100' ].data : '';
                    this.select.city     = r.data[ '1300' ] ? r.data[ '1300' ].data : '';
                    this.select.area     = r.data[ '1400' ] ? r.data[ '1400' ].data : '';
                    if ( !value ) {
                        this.group    = r.data[ '1000' ] ? r.data[ '1000' ].select : '';
                        this.province = r.data[ '1100' ] ? r.data[ '1100' ].select : '';
                        this.city     = r.data[ '1300' ] ? r.data[ '1300' ].select : '';
                        this.area     = r.data[ '1400' ] ? r.data[ '1400' ].select : '';
                    }
                } else {
                    if ( !r.data.length ) {
                        this.select[ this.regionMap[ type ] ] = null;
                    } else {
                        this.select[ this.regionMap[ type ] ] = r.data;
                    }
                }
            }
        })
    }
}

export default Controller;